package com.secondGroup.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.secondGroup.entity.ElectronicAccount;
import com.secondGroup.entity.User;
import com.secondGroup.service.ElectronicAccountService;
import com.secondGroup.service.UserService;
import com.secondGroup.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ws
 * @since 2022-05-05
 */
@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;
    @Autowired
    ElectronicAccountService electronicAccountService;

    /**
     * 跳回首页的接口
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpSession session,Model model){
        User user = (User) session.getAttribute("userInfo");
        model.addAttribute("code",1);
        model.addAttribute("userInfo",user);
        if(user.getEId()!=null){
        ElectronicAccount electronicAccount = electronicAccountService.getOne(new QueryWrapper<ElectronicAccount>()
                .lambda()
                .eq(ElectronicAccount::getEId,user.getEId()));
            model.addAttribute("eMoney",electronicAccount.getEMoney());
        }
        return "index";
    }

    @PostMapping("/login")
    public String userLogin(String account, String loginPassword, Model model, HttpServletRequest response, HttpSession session) {
        User user = new User();
        user.setAccount(account);
        user.setLoginPassword(MD5Utils.getMD5Utils(loginPassword));
        HashMap<Integer, Object> userLoginHashMap = userService.userLogin(user,response);
        if (userLoginHashMap.get(1)==null){
            model.addAttribute("code",0);
            model.addAttribute("href","/e/login.html");
            model.addAttribute("message",userLoginHashMap.get(0));
            return "error1";
        }else {
            List<User> userList = (List<User>) userLoginHashMap.get(1);
            User user1 = userList.get(0);
            session.setAttribute("userInfo",user1);
            model.addAttribute("code",1);
            model.addAttribute("userInfo",user1);
            if(user1.getEId()!=null){
                ElectronicAccount ele = electronicAccountService.getOne(new QueryWrapper<ElectronicAccount>()
                        .lambda()
                        .eq(ElectronicAccount::getEId, user1.getEId()));
                model.addAttribute("eMoney",ele.getEMoney());
            }
            return "index";
        }
    }


    @PostMapping("/logon")
    public String userLogon(String name, String phone, String email,String account, String loginPassword, String loginPasswordagen, Model model, HttpServletRequest response,HttpSession session) {
        if (!loginPassword.equals(loginPasswordagen)){
            model.addAttribute("code",0);
            model.addAttribute("message","两次密码不一致");
            return "error1";
        }
        User user = new User();
        user.setName(name);
        user.setPhone(phone);
        user.setEmail(email);
        user.setAccount(account);
        user.setLoginPassword(MD5Utils.getMD5Utils(loginPassword));
        HashMap<Integer, Object> userLoginHashMap = userService.userLogon(user,response);
        if (userLoginHashMap.get(1)==null){
            model.addAttribute("code",0);
            model.addAttribute("href","/e/logon.html");
            model.addAttribute("message",userLoginHashMap.get(0));
            return "error1";
        }else {
            session.setAttribute("userInfo",userLoginHashMap.get(1));
            model.addAttribute("code",1);
            model.addAttribute("message","注册成功");
            return "seccess";
        }

    }

    @PostMapping("/cerificationCode")
    public String getCerificationCode(String phone,Model model) {
        HashMap<Integer, Object> cerificationCodeMap = userService.getCerificationCode(phone);
        if (cerificationCodeMap.get(1)==null){
            model.addAttribute("code",0);
            model.addAttribute("message",cerificationCodeMap.get(0));
            model.addAttribute("href","/e/logon.html");
            return "error1";
        }else {
            model.addAttribute("code",cerificationCodeMap.get(1));
            model.addAttribute("phone",cerificationCodeMap.get(2));
            return "cerificationCode";
        }
    }

    @PostMapping("/phoneLogin")
    public String phoneLogin(String phone,String code,Model model,HttpSession session){
        HashMap<Integer, Object> cerificationCodeMap = userService.phoneLogin(phone,code);
        if (cerificationCodeMap.get(1)==null){
            model.addAttribute("code",0);
            model.addAttribute("message","验证码错误，请重新获取验证码");
            model.addAttribute("href","/e/login.html");
            return "error1";
        }else {
            List<User> userList = (List<User>) cerificationCodeMap.get(1);
            User user1 = userList.get(0);
            session.setAttribute("userInfo",user1);
            model.addAttribute("code",1);
            model.addAttribute("message","登录成功");
            return "seccess";
        }
    }

    @RequestMapping("/loginHtml")
    public String loginHtml(){
        return "login.html";
    }
    @RequestMapping("/logonHtml")
    public String logonHtml(){
        return "logon.html";
    }
}
